var PaymentRecord = {
    id: "PaymentRecordTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

PaymentRecord.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerSexHead', field: 'customerSexHead ', visible: false, align: 'center', valign: 'middle'},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' style='display: flex;align-items: center;'><img src='" + row.customerSexHead + "' alt='' width='40px'><span class='portraitHeadName'>" + value + "</span></a>"
            }
        },
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true, width: 135},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true, width: 80,
            formatter: function (value, row, index) {
                return value === 1 ? '男' : value === 2 ? '女' : '未知';
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true, width: 80},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '结账金额', field: 'actualAccountBalance', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '结账方式', field: 'checkoutMethod', align: 'center', valign: 'middle', sortable: true, width: 100},
        {
            title: '结账时间', field: 'rechargeTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {
            title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true, width: 100,
            formatter: function (value, row, index) {
                return '<span title="' + value + '">' + value + '</span>'
            },
            cellStyle: function (value, row, index) {
                return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
            }
        }
    ]
    return columns;
};

PaymentRecord.formParams = function () {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    var checkoutMethod = $('#checkoutMethod').val();
    var str1 = "";
    if (checkoutMethod != null && checkoutMethod != "") {
        for (var i = 0; i < checkoutMethod.length; i++) {
            if (i == (checkoutMethod.length - 1)) {
                str1 += checkoutMethod[i];
            } else {
                str1 += checkoutMethod[i] + ",";
            }
        }
    }
    queryData['checkoutMethod'] = str1;
    queryData['doctor'] = $("#doctor").val();
    queryData['fieldTc'] = $("#tcName").val();
    queryData['reach'] = $("#doctorTcInputPublics", parent.document).val().trim();
    queryData['abbreviation'] = $("#abbreviation", parent.document).val();
    var customerStatus = $('#customerStatus').val();
    var str3 = "";
    if (customerStatus != null && customerStatus != "") {
        for (var i = 0; i < customerStatus.length; i++) {
            if (i == (customerStatus.length - 1)) {
                str3 += customerStatus[i];
            } else {
                str3 += customerStatus[i] + ",";
            }
        }
    }
    queryData['customerStatus'] = str3;
    return queryData;
};

PaymentRecord.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};

PaymentRecord.check = function () {// 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        PaymentRecord.seItem = selected[0];
        return true;
    }
};

PaymentRecord.resetSearch = function () {
    $("#startTime").val("");
    $("#endTime").val("");
    $("#checkoutMethod").val("");
    $("#doctor").val("");
    $("#customerStatus").val("");
    $("#doctorTcInputPublics", parent.document).val("");
    PaymentRecord.search();
};

var parseParam = function (param) {
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i)
    });
    return paramStr.substr(1)
};
var getSingleData = function (param, key) {
    return key + "=" + param[key]
};

PaymentRecord.paymentRecordExport = function () {
    var param = parseParam(PaymentRecord.formParams());
    location.href = Feng.ctxPath + '/paymentRecord/paymentRecordExport?' + param;
};

PaymentRecord.toUpdate = function () {//修改结账方式
    if (this.check()) {
        if( PaymentRecord.seItem.checkoutMethod=='储值抵扣'){
            alert("此结账方式不能修改！");
            return;
        }
        var customerName = PaymentRecord.seItem.customerName;
        var id = PaymentRecord.seItem.id;
        var ci = customerName + "," + id;
        var index = layer.open({
            type: 2,
            title: '修改结账方式',
            area: ['40%', '50%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/paymentRecord/toUpdate/' + ci
        });
        this.layerIndex = index;
    }
};

PaymentRecord.search = function () {
    PaymentRecord.table.refresh({query: PaymentRecord.formParams()});
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + '/paymentRecord/sumBorrowAmount',
        data: PaymentRecord.formParams(),
        dataType: "json",
        success: function (data) {
            $('#sumMoney').html("(" + data.borrowAmount_sum + ")元");
        }
    });
};

$("#doctorTcSearchAlls", parent.document).click(function () {//主页搜索
    PaymentRecord.search();
});

var AccountsTime = $("#sttleAccountsTime").val();
PaymentRecord.today = function () {//今日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),0) + " " + AccountsTime)
    }
    $("#timeValue").val('1');
    PaymentRecord.search();
};

PaymentRecord.yesterday = function () {//昨日
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59")
    } else {
        $("#startTime").val(setDateTime(new Date(),2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),1) + " " + AccountsTime)
    }
    $("#timeValue").val('2');
    PaymentRecord.search();
};

PaymentRecord.thisWeek = function () {//本周
    var date = new Date();
    var currentWeek = date.getDay();
    var stepSunDay = -currentWeek + 1;
    if (currentWeek == 0) {
        stepSunDay = -7;
    }
    var stepMonday = 7 - currentWeek;
    var time = date.getTime();
    var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
    var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setDateTime(monday,1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday,0) + " " + AccountsTime)
    }
    $("#timeValue").val('3');
    PaymentRecord.search();
};

PaymentRecord.thisMonth = function () {//本月
    if ($("#naturalDaySearch").val() == "1") {
        $("#startTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    } else {
        $("#startTime").val(setMonthTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(),0) + " " + AccountsTime);
    }
    $("#timeValue").val('4');
    PaymentRecord.search();
};

PaymentRecord.naturalDaySearch = function () {//自然时间
    $("#naturalDaySearch").val('1');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),0) + " " + "23:59:59");
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + "00:00:00");
        $("#endTime").val(setDateTime(new Date(),1) + " " + "23:59:59");
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {stepSunDay = -7;}
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday,0) + " " + "00:00:00");
        $("#endTime").val(setDateTime(sunday,0) + " " + "23:59:59");
    }
    if ($("#timeValue").val() == "4") {
        $("#startTime").val(firstMonthTime(new Date(),0) + " " + "00:00:00");
        $("#endTime").val(setMonthTime(new Date(),0) + " " + "23:59:59");
    }
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    PaymentRecord.search();
};

PaymentRecord.sttleAccountsSearch = function () {// 扎帐时间
    $("#naturalDaySearch").val('2');
    if ($("#timeValue").val() == "1") {
        $("#startTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),0) + " " + AccountsTime);
    } else if ($("#timeValue").val() == "2") {
        $("#startTime").val(setDateTime(new Date(),2) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(),1) + " " + AccountsTime);
    } else if ($("#timeValue").val() == "3") {
        var date = new Date();
        var currentWeek = date.getDay();
        var stepSunDay = -currentWeek + 1;
        if (currentWeek == 0) {stepSunDay = -7;}
        var stepMonday = 7 - currentWeek;
        var time = date.getTime();
        var monday = new Date(time + stepSunDay * 24 * 3600 * 1000);
        var sunday = new Date(time + stepMonday * 24 * 3600 * 1000);
        $("#startTime").val(setDateTime(monday,1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(sunday,0) + " " + AccountsTime)
    }
    if ($("#timeValue").val() == "4") {
        $("#startTime").val(setMonthTime(new Date(),1) + " " + AccountsTime);
        $("#endTime").val(setMonthTime(new Date(),0) + " " + AccountsTime);
    }
    $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick');
    PaymentRecord.search();
};

function phonePrivileges(){   //隐藏手机号
    var phonePrivileges=parent.$("#phonePrivileges").val();
    if(phonePrivileges == 2){
        $('#PaymentRecordTable').bootstrapTable('hideColumn', 'customerPhone');
    }
}

$(function () {
    var defaultColunms = PaymentRecord.initColumn();
    var table = new BSTable(PaymentRecord.id, "/paymentRecord/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(PaymentRecord.formParams());
    PaymentRecord.table = table.init();
    PaymentRecord.today();
    menuNameHideColumn($("#menuName").val(),'PaymentRecordTable');
    checkDate = function () {PaymentRecord.search()};
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {PaymentRecord.search()});
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    phonePrivileges();
});